﻿using _5E.Infrastructure.Collections;
using StoneAge.Contract;
using StoneAge.Controller.Request;
using StoneAge.Controller.Routing;
using StoneAge.DataContract.Messaging;

namespace StoneAge.Controller.Handlers
{
    class ChooseRoleHandler: PostResponseRequestHandler
    {
        private IRoleService m_roleService = null;

        public ChooseRoleHandler(IRoleService roleService)
            : base(Routes.CHOOSE_ROLE)
        {
            m_roleService = roleService;
        }

        protected override ResponseBase GetResponseBy(WebRequest webRequest)
        {
            ChooseRoleRequest request = new ChooseRoleRequest
            {
                AccountId = webRequest.User.AccountId,
                IsLeft = webRequest.QueryArguments.ExtractFor<bool>("isLeft")
            };
            ChooseRoleResponse response = m_roleService.ChooseBy(request);
            if (response.Success)
            {
                webRequest.User.RoleId = response.RoleId;
                webRequest.User.Save();
            }
            return response;
        }
    }
}
